package com.murray.assignment.controller;

import com.github.pagehelper.PageInfo;
import com.murray.assignment.pojo.Category;
import com.murray.assignment.pojo.Goods;
import com.murray.assignment.pojo.SkuGoods;
import com.murray.assignment.service.GoodsService;
import com.murray.assignment.service.SkuGoodsService;
import com.murray.assignment.utils.SolrPages;
import org.apache.ibatis.annotations.Param;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.awt.desktop.QuitResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Murray
 * @Date 2021/6/12
 * @Time 22:38
 */
@Controller
@RequestMapping("/products")
public class ProductsController {

    //solr客户端
    @Autowired
    private HttpSolrClient httpSolrClient;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private SkuGoodsService skuGoodsService;

    @RequestMapping("/presentationByCid")
    /**
     * @author Murray
     * @param presentPage 传入当前页面，如果没有默认值为1
     * @param pageSize  传入分页的数量，就是每一页有多少条数据，默认4条
     */
    public ModelAndView presentationByCid(
            Integer cid,
            @RequestParam(value = "presentPage", defaultValue = "1") Integer presentPage,
            @RequestParam(value = "pageSize", defaultValue = "4") Integer pageSize
//            @RequestParam(value = "categoryList",defaultValue = "") List<Category> categoryList

    ) {

        PageInfo<Goods> pageInfo = goodsService.findGoodsByCid(cid, presentPage, pageSize);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.setViewName("/search");
        modelAndView.addObject("cid", cid);
//        modelAndView.addObject("categoryList",categoryList);
        return modelAndView;
    }

    @RequestMapping("/presentationBySolr")
//    通过solr客户端查询
    public ModelAndView presentationBySolr(
            @RequestParam(value = "cid", defaultValue = "0") Integer cid,
            @RequestParam(value = "presentPage", defaultValue = "1") Integer presentPage,
            @RequestParam(value = "pageSize", defaultValue = "4") Integer pageSize,
            @RequestParam(value = "keyword", defaultValue = "") String keyword
    ) {
//        创建一个solr查询对象
        SolrQuery solrQuery = new SolrQuery();
//        设置默认的查询字段
        solrQuery.set("df", "goodsName");
//        设置默认查询条件
        if (keyword.equals(""))
            solrQuery.setQuery("*");
        else
            solrQuery.setQuery(keyword + "*");
//        设置过滤条件
        if (cid != 0)
            solrQuery.setFilterQueries("cid:" + cid);

//          组装字段
        solrQuery.addField("id");
        solrQuery.addField("goodsName");
        solrQuery.addField("salesPrice");
        solrQuery.addField("goodsImg");
        solrQuery.addField("salesNum");

//        设置查询分页数据，说白了设置偏移量
        solrQuery.setStart((presentPage - 1) * pageSize);
        solrQuery.setRows(pageSize);
//        将查询对象传递到服务器
        try {
            QueryResponse queryResponse = httpSolrClient.query(solrQuery);
            httpSolrClient.commit();
            //所有查询到当前页商品信息字段
            List<Goods> listGoods = queryResponse.getBeans(Goods.class);


            SolrPages<Goods> solrPages = new SolrPages<>();
            solrPages.setPageIndex(presentPage);
            solrPages.setPageSize(pageSize);
            solrPages.setRecordCount((int) queryResponse.getResults().getNumFound());
            solrPages.setGoodsList(listGoods);
            //计算出总页数
            solrPages.setPageCount((int) Math.ceil(solrPages.getRecordCount() / (double) pageSize));

            System.out.println(solrPages.getPageCount());

            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("solrPages", solrPages);
            modelAndView.addObject("cid", cid);
            modelAndView.addObject("keyword", keyword);
            modelAndView.setViewName("/searchBySolr");
            return modelAndView;
        } catch (SolrServerException | IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    @RequestMapping("/presentationByGoodsId")
    public ModelAndView presentationByGoodsId(Integer goodsId) {
        Goods goods = goodsService.selectByGoodsId(goodsId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("goods", goods);
        modelAndView.setViewName("/goodsDetail");
        return modelAndView;
    }

    /**
     * @param skuProperties 传入sku商品属性
     * @param goodsId       传入商品id
     * @return 返回一个sku商品对象
     */
    @RequestMapping(value = "/getSkuGoods",method = RequestMethod.GET)
    @ResponseBody
    public SkuGoods getSkuGoods(@Param(value = "skuProperties") String skuProperties, @Param(value = "goodsId") Integer goodsId) {
        return skuGoodsService.getSkuGoods(skuProperties, goodsId);
    }

}
